#include "chartform.h"
#include "ui_chartform.h"
#include <QDebug>
#include <QToolTip>
#include <QFileDialog>

#include <QtConcurrent/QtConcurrent>


chartForm::chartForm(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::chartForm)
{
    ui->setupUi(this);
    chart->legend()->hide();

    QFont font;
    font.setFamily("Arial");
    font.setPointSize(12);

    xAxis->setTitleText("X");//标题
    xAxis->setTitleFont(font);
    xAxis->setTickCount(6); //主分隔个数：0到10分成20个单位
    xAxis->setMinorTickCount(4); //每个单位之间绘制了多少虚网线
    xAxis->setLabelsFont(font);
    xAxis->setLabelFormat("%.3f"); //标签格式：每个单位保留几位小数

    yAxis->setTitleText("Y");//标题
    yAxis->setTitleFont(font);
    yAxis->setTickCount(6); //主分隔个数：0到10分成20个单位
    yAxis->setMinorTickCount(4); //每个单位之间绘制了多少虚网线
    yAxis->setLabelsFont(font);
    yAxis->setLabelFormat("%.3f"); //标签格式：每个单位保留几位小数


    ui->chartView->setChart(chart);

    if(ui->buttonWidget->layout())
        delete ui->buttonWidget->layout();

    ui->buttonWidget->setLayout(layout);

    showBar(false);
}

chartForm::~chartForm()
{
    delete ui;
}

void chartForm::setTitleFont(AXIS ss, QFont font){
    if(ss==X_AXIS)
        xAxis->setTitleFont(font);
    else
        yAxis->setTitleFont(font);
}
void chartForm::setTickCount(AXIS ss, int TickCount){
    if(ss==X_AXIS)
        xAxis->setTickCount(TickCount);
    else
        yAxis->setTickCount(TickCount);
}
void chartForm::setMinorTickCount(AXIS ss, int MinorTickCount){
    if(ss==X_AXIS)
        xAxis->setMinorTickCount(MinorTickCount);
    else
        yAxis->setMinorTickCount(MinorTickCount);
}
void chartForm::setLabelsFont(AXIS ss, QFont font){
    if(ss==X_AXIS)
        xAxis->setLabelsFont(font);
    else
        yAxis->setLabelsFont(font);
}
void chartForm::setLabelFormat(AXIS ss, int LabelFormat){
    if(ss==X_AXIS)
        xAxis->setLabelFormat(QStringLiteral("%.%1f").arg(LabelFormat));
    else
        yAxis->setLabelFormat(QStringLiteral("%.%1f").arg(LabelFormat));
}
void chartForm::setLabelsAngle(AXIS ss, int Angle){
    if(ss==X_AXIS)
        xAxis->setLabelsAngle(-60);
    else
        yAxis->setLabelsAngle(Angle);
}

void chartForm::addSeries(QString s, QString x, QString y){
    //防止曲线名冲突
    int i=0;
    QString temp = s;
    while(Series.keys().contains(temp)){
        i++;
        temp=s+QStringLiteral("(%1)").arg(i);
    }
    s = temp;

    //添加曲线
    Series[s] = new QLineSeries;
    QStringList name = {x,y};
    nameMap[s] = name;
    buttonMap[s] = new QRadioButton;
    buttonMap[s]->setText(s);
    layout->addWidget(buttonMap[s]);

    connect(buttonMap[s],&QRadioButton::toggled,this,[=](bool f){
        showSeries_private(s);
    });
    chart->addSeries(Series[s]);
    chart->setAxisX(xAxis, Series[s]);
    chart->setAxisY(yAxis, Series[s]);

    connect(Series[s], &QLineSeries::hovered, [=](const QPointF &point, bool state)mutable{
        if (state) QToolTip::showText(QCursor::pos(), QString("(%1,%2)").arg(point.x()).arg(point.y()));
    });

    //只允许显示一条曲线
    showSeries_private(s);

    //图标中有多条曲线，打开曲线切换选项
    if(Series.count()>1)
        showBar(true);

}

void chartForm::setSeriesPen(QString SeriesTitle, QPen pen){
    Series[SeriesTitle]->setPen(pen);
}

void chartForm::showSeries(QString s){
    if(!buttonMap[s]->isChecked()){
        buttonMap[s]->click();
    }
}

void chartForm::showSeries_private(QString s){

    if(!chart->series().isEmpty())
    {
        for(int i = 0;i<chart->series().length();i++)
            chart->series()[i]->setVisible(false);
    }

    Series[s]->setVisible(true);
    yAxis->setTitleText(nameMap[s][0]);//标题
    xAxis->setTitleText(nameMap[s][1]);//标题
}

void chartForm::getSeries(QString &SeriesTitle, QLineSeries *&ss){
    if(Series.keys().contains(SeriesTitle))
        ss = Series[SeriesTitle];
    else
        ss =new QLineSeries;
}
void chartForm::getCurrentSeries(QString &SeriesTitle,QLineSeries *&ss){
    foreach (QString key, Series.keys()) {
        if(Series[key]->isVisible()){
            SeriesTitle = key;
            ss = Series[SeriesTitle];
            break;
        }
    }
}

void chartForm::shortShow(int pointsCounts){
    showCounts = pointsCounts;
}

void chartForm::longShow(){
    showCounts = -1;
}

void chartForm::addPoint(QString s , QPointF f){
    *Series[s]<<f;

    if(showCounts>0&&Series[s]->count()>showCounts){
        Series[s]->remove(0);
    }

    if(Series[s]->isVisible())
        updateAxisRanges(Series[s]);
    update();
}



void chartForm::addPoints(QString s, QList<QPointF>fl){
    foreach (QPointF f, fl) {
        addPoint(s,f);
    }
}

void chartForm::clearSeries(QString s ){
    Series[s]->clear();
}

void chartForm::clearAll(){
    foreach (QString s, Series.keys()) {
        clearSeries(s);
    }
}

void chartForm::setMeaaage(QString m ){
    ui->counts->setText(m);
}

void chartForm::showBar(bool flag){
    if(flag)
        ui->widget_bar->show();
    else
        ui->widget_bar->hide();
}

void saveCsv(QString path,QMap<QString, QLineSeries*> ss){

    QFile file(path);
    if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
        qWarning("Cannot open file for writing: %s", qPrintable(file.errorString()));
        return;
    }

    QTextStream out(&file);

    int maxCounts = 0;

    foreach (QString key, ss.keys()){
        out << key+"-X,"<<key+"-Y,";
        maxCounts=maxCounts>ss[key]->count()?maxCounts:ss[key]->count();
    }
    out <<"\n";

    for (int i =0;i<maxCounts;i++){
        foreach (QString key, ss.keys()){
            QLineSeries *s = ss[key];
            if(i<s->count()){
                out <<s->points()[i].x()<< ','<<s->points()[i].y();
            }
        }
        out <<"\n";
    }
    file.close();
}


void chartForm::saveAllPoints(const QString defualtName){
    QString saveFilePath = QFileDialog::getSaveFileName(
        this,
        tr("Save File"),
        defualtName,
        defualtName+"(*.csv)");

    QFuture<void> future = QtConcurrent::run(saveCsv,saveFilePath,Series);
    future.waitForFinished();
}

